//
// Created by Administrator on 2025/9/16.
//

#ifndef FATFS_NORFLASH_H
#define FATFS_NORFLASH_H
#include "main.h"
#include "spi.h"
#include "dma.h"
#define FLASH_WriteEnable           0x06
#define FLASH_WriteDisable          0x04
#define FLASH_ReadStatusReg1        0x05
#define FLASH_ReadStatusReg2        0x35
#define FLASH_ReadStatusReg3        0x15
#define FLASH_WriteStatusReg1       0x01
#define FLASH_WriteStatusReg2       0x31
#define FLASH_WriteStatusReg3       0x11
#define FLASH_ReadData              0x03
#define FLASH_FastReadData          0x0B
#define FLASH_FastReadDual          0x3B
#define FLASH_FastReadQuad          0xEB
#define FLASH_PageProgram           0x02
#define FLASH_PageProgramQuad       0x32
#define FLASH_BlockErase            0xD8
#define FLASH_SectorErase           0x20
#define FLASH_ChipErase             0xC7
#define FLASH_PowerDown             0xB9
#define FLASH_ReleasePowerDown      0xAB
#define FLASH_DeviceID              0xAB
#define FLASH_ManufactDeviceID      0x90
#define FLASH_JedecDeviceID         0x9F
#define FLASH_Enable4ByteAddr       0xB7
#define FLASH_Exit4ByteAddr         0xE9
#define FLASH_SetReadParam          0xC0
#define FLASH_EnterQPIMode          0x38
#define FLASH_ExitQPIMode           0xFF

#define NORFLASH_CS_PIN             GPIO_PIN_14
#define NORFLASH_CS_GPIO_PORT       GPIOB
#define NORFLASH_CS_OUT(x)          do{x?HAL_GPIO_WritePin(NORFLASH_CS_GPIO_PORT, NORFLASH_CS_PIN, GPIO_PIN_SET):\
                                        HAL_GPIO_WritePin(NORFLASH_CS_GPIO_PORT, NORFLASH_CS_PIN, GPIO_PIN_RESET);}while(0)
uint16_t norflash_getID(void);
void norflash_init(void);
uint8_t norflash_wr_byte(uint8_t txdata);
void norflash_set_speed(uint32_t speed);
void norflash_page_write(uint32_t addr,uint8_t *buf,uint16_t len);
void norflash_read(uint32_t addr,uint8_t *buf,uint16_t len);
void norflash_wait_ready(void);
void norflash_sector_erase(uint32_t addr);
void norflash_write_api(uint32_t addr, uint8_t *buf, uint16_t len);
void norflash_write(uint32_t addr, uint8_t *buf, uint16_t len);
volatile bool get_norflash_tx_cplt_flag(void);
uint8_t norflash_read_status(void);
uint8_t norflash_test_run(void);
#endif //FATFS_NORFLASH_H